#include "detectiontab.h"
#include "ui_detectiontab.h"

DetectionTab::DetectionTab(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::DetectionTab)
{
    m_ui->setupUi(this);
    imageLoader = new ImageLoader();
}

DetectionTab::~DetectionTab()
{
    delete m_ui;
    delete pOpener;
    delete imageLoader;
    delete imgProc;
}

void DetectionTab::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void DetectionTab::populate(const QString &fileName)
{
    pOpener = new ProjectOpener(fileName);
    QStringList *families = pOpener->getFamily();
    QVector<QString> *files = pOpener->getFiles();

    imageLoader->addFiles(files);

    m_ui->familyComboBox->clear();
    m_ui->familyComboBox->addItem("----------");
    m_ui->familyComboBox->addItems(*families);
    connect(m_ui->familyComboBox, SIGNAL(activated(QString)),
            this, SLOT(updatePDN(QString)));

    m_ui->start_label->setText(pOpener->getStartTime());
    m_ui->info_text->setText(*pOpener->getProjInfo());
}

void DetectionTab::updatePDN(QString family)
{
    if(!pOpener->isOpen())
        pOpener->createConnection();

    if(family != tr("----------") && family.length() > 0)
    {
        QStringList *pdnList = pOpener->getPDN(family);
        m_ui->pdnComboBox->clear();
        m_ui->pdnComboBox->addItem("----------");
        m_ui->pdnComboBox->addItems(*pdnList);
        updateChild(pdnList, 1);
        connect(m_ui->pdnComboBox, SIGNAL(activated(QString)),
                this, SLOT(updateTime(QString)));
    }
}

void DetectionTab::updateTime(QString pdn)
{
    if(!pOpener->isOpen())
        pOpener->createConnection();

    QString family = m_ui->familyComboBox->currentText();
    if(family != tr("----------") && family.length() > 0 && pdn != tr("----------") && pdn.length() > 0)
    {
        QStringList *timeList = pOpener->getTime(family, pdn);
        QStringList *pdNumber = new QStringList();
        pdNumber->append(pdn);

        m_ui->timeComboBox->clear();
        m_ui->timeComboBox->addItem("----------");
        m_ui->timeComboBox->addItems(*timeList);
        updateChild(pdNumber, 1);
        connect(m_ui->timeComboBox, SIGNAL(activated(QString)), this, SLOT(updateByTime(QString)));
    }
}

void DetectionTab::updateByTime(QString time)
{
    if(time.length() > 0 && time != tr("----------"))
    {

    }
}

void DetectionTab::addChild(const QString &file)
{
    //imageLoader->loadIcons(file);
    //m_ui->mainGridLayout->addWidget(imageLoader, 0, 0);
}

void DetectionTab::updateChild(QStringList *filter, int type)
{
    // When type==1, update icons based on pdn numbers
    // which all belong to single family/genotype
    if(type == 1)
    {
        imageLoader->updateIcons(filter);
    }
    else if(type == 2)
    {

    }
    reload();
}

// Get current image in the display image box.
QString *DetectionTab::getCurrImg()
{    
    QString *curr = imageLoader->getCurrFile();
    return curr;
}

void DetectionTab::reload()
{
    imageLoader->reload();
    m_ui->mainGridLayout->addWidget(imageLoader, 0, 0);
}

void DetectionTab::detect()
{
    QString *curr = imageLoader->getCurrFile();

    if(curr->length() > 0)
    {
        imgProc = new ImageProcessor(*curr);
        //int t = imgProc->cvDetect();
    }
    else
    {
        // Warning: no image has been uploaded yet

    }
}
